import freemarker.template.*;
import java.util.*;
import java.io.*;

public class CodeGenerator {

    public static void main(String[] args) throws Exception {
    	if(args.length < 3){
    		System.out.println("Usage: jp.bat -xml -template -outfile");
    		System.exit(0);
    	}
    	
    	String dataModel = args[0];
    	String template = args[1];
    	String outfile = args[2];
		String dataModel2 ="";
		String dataModel3 ="";
		if( args.length>3)
	    	dataModel2 = args[3];
		if( args.length>4)
	    	dataModel3 = args[4];
    	
        /* Adjust the configuration */
        Configuration cfg = Configuration.getDefaultConfiguration();
        cfg.setDirectoryForTemplateLoading(
                new File("."));

        /* Create a template */
        Template temp = cfg.getTemplate(template);

        /* Create a data model */
        Map root = new HashMap();
		root.put(
        "doc",
        freemarker.ext.dom.NodeModel.parse(new File(dataModel)));  
		if(dataModel2 != "" && !dataModel2.equals(""))
		{
			root.put(
			"sec",
			freemarker.ext.dom.NodeModel.parse(new File(dataModel2)));
		}
		if(dataModel3 != "" && !dataModel3.equals(""))
		{
			root.put(
			"objectName",
			new GetArg(dataModel3));
 		}
       FileOutputStream fout = new FileOutputStream(outfile);
        /* Merge data model with template */
        Writer out = new OutputStreamWriter(fout);
        //Writer out = new OutputStreamWriter(System.out);
        temp.process(root, out);
        out.flush();
        fout.close();
    }

}   
  
   